// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview'
import 'iview/dist/styles/iview.css'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import API from '@/api'
import axios from 'axios'

Vue.use(iView)
Vue.use(ElementUI)

Vue.prototype.API = API
Vue.prototype.axios = axios
Vue.config.productionTip = false
axios.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded'
// axios.defaults.headers.common['token'] = sessionStorage.getItem('token')

router.beforeEach((to, from, next) => {
  // 如果本身就在登录页面则不校验
  if (to.path === '/login') {
    next()
  }
  // 如果没有登录态则跳登录页
  if (!sessionStorage.getItem('token')) {
    return next({ path: '/login' })
  }
  next()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
